package oocl.ita.eordering.controller;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.Client;
import javax.ws.rs.core.MediaType;

import oocl.ita.eordering.po.Food;
import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.ResponsePo;
import oocl.ita.eordering.service.FoodService;
import oocl.ita.eordering.util.JSONUtil;

@Controller
@RequestMapping("/homes")
public class FoodController {
	@Autowired
	private FoodService fs;

	@Autowired
	JmsTemplate template;

	@Autowired(required = true)
	@Qualifier(value = "recfoodQueueDestination")
	Destination dest;

	// 页面跳转：菜品列表，加载所有菜品到视图
	@RequestMapping("/allDish")
	public ModelAndView allDish(HttpServletRequest req) {
		ModelAndView mov = new ModelAndView();
		Merchant m = (Merchant) req.getSession().getAttribute("user");
		mov.addObject("foods", fs.findFoodsByMId(m.getmId()));
		mov.addObject("mainPage", "listDish.jsp");
		mov.setViewName("home");
		return mov;
	}

	// 页面跳转：获取菜品信息-----弃置,只做页面跳转用----直接将页面数据带到update页面，不查数据库
	@RequestMapping("/linkEditFood")
	public ModelAndView linkEditFood(HttpServletRequest req, Integer fId) {
		ModelAndView mov = new ModelAndView();
		// Merchant m = (Merchant)req.getSession().getAttribute("user");
		// fs.findFoodsByMId(m.getmId());
		mov.addObject("mainPage", "editDish.jsp");
		mov.setViewName("home");
		return mov;
	}

//	// 表单提交：修改菜品信息,修改成功后返回菜品列表页
//	@RequestMapping("editFood")
//	public String editFood(HttpSession sen, Integer fId, String fName, Double fPrice, String fViewPath,
//			MultipartFile foodPic) {
//		String mTel = ((Merchant) sen.getAttribute("user")).getmTel();
//		if (foodPic.getSize() != 0) {
//			fViewPath = fs.getFoodViewPath(mTel, foodPic, fName, fPrice);
//		}
//		if (fPrice == null) {
//			fPrice = 0.0;
//		}
//		fs.updateFood(fId, fName, fPrice, fViewPath);
//		return "redirect:/homes/allDish";
//	}

	// 页面跳转：添加菜品页面
	@RequestMapping("/linkAdd")
	public ModelAndView addFoods() {
		ModelAndView mov = new ModelAndView();
		mov.addObject("mainPage", "addDish.jsp");
		mov.setViewName("home");
		return mov;
	}

	// 表单提交：提交添加的菜品信息，成功后返回菜品列表页
	@RequestMapping("/addDish")
	public String addDish(HttpSession sen, String fName, Double fPrice) {
		Merchant m = (Merchant) sen.getAttribute("user");
		int mId = m.getmId();
		String fViewPath = (String)sen.getAttribute("fViewPath");
		Food f = new Food(fName, mId, fPrice, fViewPath, 1);
		fs.addFood(f);
		sen.setAttribute("fViewPath", "");
		
		Client client = Client.create();
		client.setReadTimeout(1000);
		WebResource wr = client.resource("http://10.222.232.152:8082/WeekFourClient/merchantStatusChange");
		String msg = wr.accept(MediaType.APPLICATION_JSON_TYPE)// accept请求头
		.get(String.class);
		System.out.println("orderMsg is ..." + msg);
		return "redirect:/homes/allDish";

	}

	// 按钮表单提交：删除菜品,删除成功后返回菜品列表页(可异步)
	@RequestMapping("/delDish")
	@ResponseBody
	public ResponsePo delDish(Integer fId) {
		ResponsePo delDish = new ResponsePo();
		boolean i = fs.deleteFood(fId);
		delDish.setStatus(i);
		return delDish;
	}

	// 按钮表单提交:推荐菜品,发送JMS，推荐成功后为已推荐状态，返回列表页（可异步）
	@RequestMapping("/recommendFood")
	@ResponseBody
	public ResponsePo recommendFood(Integer fId, String fName, String fViewPath, HttpSession session)
			throws JsonProcessingException {
		System.out.println(fId+"....................");
		ResponsePo recRes = new ResponsePo();
		boolean i=fs.updateStatus(fId);
		recRes.setStatus(i);
		
		Integer mId = ((Merchant) session.getAttribute("user")).getmId();
		ita.project4.main.protocol.po.Food Recomfood = new ita.project4.main.protocol.po.Food(fId, fName, mId,
				fViewPath, 1);
		// JMS把菜品发送A端
		// ApplicationContext context=new
		// ClassPathXmlApplicationContext("applicationContext.xml");
		// JmsTemplate template=(JmsTemplate)context.getBean("jmsTemplate");
		// Destination
		// dest=(Destination)context.getBean("recfoodQueueDestination");
		String info = JSONUtil.objetc2Json(Recomfood);// 转成JSON格式发送
		template.send(dest, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage msg = session.createTextMessage(info);
				return msg;
			}
		});

		return recRes;
	}

}
